package com.brillilab.service.core.kit.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.kit.ReagentMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.kit.Reagent;
import com.brillilab.service.core.kit.IReagentService;

@Service
@Transactional
public class ReagentServiceImpl implements IReagentService {
	@Autowired
	private ReagentMapper reagentMapper;

	@Override
	public Reagent insert(Reagent entity) {
		Assert.isTrue(StringUtils.isNotBlank(entity.getName())&& entity.getUnit()!=null 
				&& entity.getZone()!=null && entity.getReagentTypeId()!=null ,"试剂信息不全");
		Integer insert = reagentMapper.insert(entity);
		Assert.isTrue(insert == 1, "添加失败");
		return entity;
	}

	@Override
	public boolean deleteById(Long id) {
		Assert.notNull(id, "删除ID不能为空");
		Reagent entity = new Reagent();
		entity.setId(id);
		entity.setIsDelete(IsDeleteEnum.YES.getValue());
		return this.updateById(entity);
	}

	@Override
	public boolean updateById(Reagent entity) {
		entity.setUpdateTime(new Date());
		Integer b = reagentMapper.updateById(entity);
		return b == 1 ? true : false;
	}

	@Override
	public Reagent selectById(Long id) {
		Reagent reagent = reagentMapper.selectById(id);
		return reagent;
	}

	@Override
	public List<Reagent> selectList(Long kitId) {
		LambdaQueryWrapper<Reagent> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Reagent::getKitId, kitId);
		wrapper.eq(Reagent::getIsDelete, IsDeleteEnum.NO.getValue());
		List<Reagent> list = reagentMapper.selectList(wrapper);
		return list;
	}

	@Override
	public List<Reagent> selectListAllState(Long kitId) {
		LambdaQueryWrapper<Reagent> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Reagent::getKitId, kitId);
		List<Reagent> list = reagentMapper.selectList(wrapper);
		return list;
	}

	@Override
	public List<Reagent> selectByIds(List<Long> kitIds) {
		if(!CollectionUtils.isEmpty(kitIds)) {
			LambdaQueryWrapper<Reagent> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(Reagent::getIsDelete, IsDeleteEnum.NO.getValue());
			wrapper.in(Reagent::getKitId, kitIds);
			return reagentMapper.selectList(wrapper);
		}
		return null;
	}

    @Override
    public List<Reagent> selectByReagentIds(List<Long> reagentIds) {
		if(!CollectionUtils.isEmpty(reagentIds)) {
			LambdaQueryWrapper<Reagent> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(Reagent::getId, reagentIds);
			return reagentMapper.selectList(wrapper);
		}
		return new ArrayList<>(0);
    }

    @Override
    public int updateAliasBatch(List<Reagent> reagents) {
		if(!CollectionUtils.isEmpty(reagents)){
			return reagentMapper.updateAliasBatch(reagents);
		}
		return 0;
    }

	@Override
	public int updateBatch(List<Reagent> reagents) {
		if(!CollectionUtils.isEmpty(reagents)){
			return reagentMapper.updateBatch(reagents);
		}
		return 0;
	}

}
